package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.ParameterConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.domain.dao.WorkOrderProductDao;
import com.xbongbong.saas.domain.entity.BusinessProductEntity;
import com.xbongbong.saas.domain.entity.WorkOrderProductEntity;
import com.xbongbong.saas.model.WorkOrderProductModel;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author 刘阳
 * @date 2019/4/23 4:20 PM
 * @since v1.0
 * @version v1.0
 */
@Service("workOrderProductModel")
public class WorkOrderProductModelImpl implements WorkOrderProductModel {

    @Resource
    private WorkOrderProductDao workOrderProductDao;

    @Override
    public Integer insert(WorkOrderProductEntity entity) throws XbbException {
        long now = DateTimeUtil.getInt();
        entity.setAddTime(now);
        entity.setUpdateTime(now);
        return workOrderProductDao.insert(entity);
    }

    @Override
    public Integer insertBatch(List<WorkOrderProductEntity> productAddList) {
        if (CollectionUtils.isEmpty(productAddList)) {
            return 0;
        }
        long now = DateTimeUtil.getInt();
        productAddList.forEach(item->{
            item.setAddTime(now);
            item.setUpdateTime(now);
        });
        return workOrderProductDao.insertBatch(productAddList);
    }

    @Override
    public Integer update(WorkOrderProductEntity entity) throws XbbException {
        long now = DateTimeUtil.getInt();
        entity.setUpdateTime(now);
        return workOrderProductDao.update(entity);
    }

    @Override
    public List<WorkOrderProductEntity> findEntitys(Map<String, Object> param){
        return workOrderProductDao.findEntitys(param);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return workOrderProductDao.getEntitysCount(param);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    @Override
    public WorkOrderProductEntity getByKey(Long key, String corpid){
        return workOrderProductDao.getByKey(key, corpid);
    }

    @Override
    public Integer save(WorkOrderProductEntity entity) throws XbbException {
        if (entity .getId() == null || entity.getId().equals(0L)) {
            return insert(entity);
        }
        return update(entity);
    }

    @Override
    public List<WorkOrderProductEntity> getProductsByWorkOrderId(Long workOrderId, String corpid, List<Integer> delIn) {
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("corpid", corpid);
        param.put("workOrderId", workOrderId);
        param.put("delIn", delIn);
        param.put(ParameterConstant.ORDER_BY_STR, BasicConstant.SORT);
        return findEntitys(param);
    }

    @Override
    public List<WorkOrderProductEntity> getProductsByWorkOrderId(Long workOrderId, String corpid) {
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("corpid", corpid);
        param.put("workOrderId", workOrderId);
        param.put("del", DelEnum.NORMAL.getDel());
        param.put(ParameterConstant.ORDER_BY_STR, StringConstant.SAAS_LINK_BUSINESS_ID);
        return findEntitys(param);
    }

    @Override
    public Integer deleteByWorkOrderIdIn(List<Long> workOrderIdIn, String corpid) throws XbbException {
        Integer result = workOrderProductDao.deleteByWorkOrderIdIn(workOrderIdIn, corpid);
        return result;
    }

    @Override
    public List<BusinessProductEntity> findProductNum(Map<String, Object> param) {
        return workOrderProductDao.findProductNum(param);
    }

    @Override
    public Integer deleteBatch(List<Long> list, String corpid, Integer del) throws XbbException {
        if (CollectionUtils.isEmpty(list)) {
            return 0;
        }
        Integer integer = workOrderProductDao.deleteByIdIn(corpid, list, del);
        return integer;
    }

    @Override
    public Integer updateBatch(List<WorkOrderProductEntity> list, String corpid) {
        if (CollectionsUtil.isEmpty(list)) {
            return 0;
        }
        return workOrderProductDao.updateBatch(list, corpid);
    }

    @Override
    public Integer updateMulti(List<WorkOrderProductEntity> list, String corpid) throws XbbException {

        if (CollectionsUtil.isEmpty(list)) {
            return 0;
        }
        return workOrderProductDao.updateMulti(list, corpid);
    }

    @Override
    public Integer updateOutstockBatch(List<WorkOrderProductEntity> list, String corpid) {
        return  workOrderProductDao.updateOutstockBatch(list, corpid);
    }

    @Override
    public Integer findProductNumCount(Map<String, Object> param) {
        return workOrderProductDao.findProductNumCount(param);
    }

    @Override
    public List<BusinessProductEntity> findProductNumIncludeProcess(Map<String, Object> param) {
        return workOrderProductDao.findProductNumIncludeProcess(param);
    }

    @Override
    public Integer findProductNumCountIncludeProcess(Map<String, Object> param) {
        return workOrderProductDao.findProductNumCountIncludeProcess(param);
    }

    @Override
    public Long isProductSubAttrExist(String attr, String corpid, Long formId, Integer isArray) {
        return workOrderProductDao.isProductSubAttrExist(attr, corpid, formId, isArray);
    }

    @Override
    public Double summaryByAttr(String field,String corpid,  Long refId, List<Long> productIdIn) {
        return workOrderProductDao.summaryByAttr(field,corpid,  refId, productIdIn);
    }

    @Override
    public Double summaryByAttrFromOutSide(String field,String corpid,  Long refId, List<Long> productIdIn) {
        return workOrderProductDao.summaryByAttrFromOutSide(field,corpid,  refId, productIdIn);
    }

    @Override
    public Double calcuateByAttrFromOutSide(String field1, String field2, String corpid, Long refId, List<Long> productIdIn) {
        return workOrderProductDao.calcuateByAttrFromOutSide(field1, field2, corpid, refId, productIdIn);
    }

    @Override
    public Long countProductSub(String attr, String corpid, Long formId, Integer isArray) {
        return workOrderProductDao.countProductSub(attr, corpid, formId, isArray);
    }

    @Override
    public List<WorkOrderProductEntity> getProductWithSubData(String attr, String corpid, Long formId, Integer isArray, Long idGte, Integer limit) {
        return workOrderProductDao.getProductWithSubData(attr, corpid, formId, isArray, idGte, limit);
    }

    @Override
    public List<String> getCorpidList() {
        return workOrderProductDao.getCorpidList();
    }


}
